using System.Collections.Specialized;
using AppExtensions.Views;

namespace AppExtensions.Presenters {
  public abstract class BasePresenter<TView> where TView : IBaseView {
    public BasePresenter() {
      this.Params = new NameValueCollection();
    }
    public BasePresenter(TView view)
      : this(view, new NameValueCollection()) { }

    public BasePresenter(TView view, NameValueCollection parameters) {
      this._params = parameters;
      this.view = view;
    }
    public virtual void OnViewInitialized() { }
    public virtual void OnViewLoaded() { }
    public virtual void OnViewFirstLoaded() { }
    public virtual void OnPreRender() { }
    private TView view;

    public TView View {
      get { return view; }
      set { if (view == null) view = value; }
    }
    public T GetParam<T>(string key) {
      object o = this.Params[key];
      return (T)o;
    }
    public bool TryGetParam<T>(string key, out T value) {
      try {
        object o = this.Params[key];
        value = (T)o;
        return true;
      }
      catch {
        value = default(T);
        return false;
      }
    }
    private NameValueCollection _params;

    public NameValueCollection Params {
      get { return _params; }
      set { _params = value; }
    }
    public string this[string key] {
      get {
        return this.Params[key];
      }
      set {
        this.Params[key] = value;
      }
    }
  }
}
